package com.work.demo.utils;

import cn.hutool.core.util.URLUtil;
import lombok.Data;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;

/**
 * 正向代理封装对象，跨节点请求时使用
 *
 * @author xuanyun
 */
@Data
public class ForwardProxy {

    private final Proxy proxy;

    private String username;
    private String password;

    public ForwardProxy(String hostName, int port) {
        this.proxy = new Proxy(Type.HTTP, new InetSocketAddress(hostName, port));
    }

    public ForwardProxy(String proxyUrl) {
        URL url = URLUtil.url(proxyUrl);
        this.proxy = new Proxy(Type.HTTP, new InetSocketAddress(url.getHost(), url.getPort()));
    }

    public String getHostName() {
        return ((InetSocketAddress) proxy.address()).getHostString();
    }

    public int getPort() {
        return ((InetSocketAddress) proxy.address()).getPort();
    }

}
